package com.oyjd.framework.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 简单javaBean工具类
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */

public class Reflection {
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 给特定属性set值
	 * 
	 * @param m
	 * @param obj
	 * @param para
	 */
	public static void invoke(Method m, Object obj, String para) {
		try {
			Object npara = parseParaType(m, para);
			m.invoke(obj, new Object[] { npara });
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	/**
	 * json参数类型转化为实际方法需求类型
	 * 
	 * @param m
	 * @param para
	 * @return
	 */
	private static Object parseParaType(Method m, String para) {
		Object result = null;
		String clsName = m.getParameterTypes()[0].getName();
		if ("java.lang.String".equals(clsName)) {
			result = para;
		} else if ("java.util.Date".equals(clsName)) {
			try {
				result = sdf.parse(para);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else if ("boolean".equals(clsName) || "java.lang.Boolean".equals(clsName)) {
			result = Boolean.parseBoolean(para);
		} else if ("int".equals(clsName) || "java.lang.Integer".equals(clsName)) {
			result = Integer.parseInt(para);
		} else if ("float".equals(clsName) || "java.lang.Float".equals(clsName)) {
			result = Float.parseFloat(para);
		} else if ("double".equals(clsName) || "java.lang.Double".equals(clsName)) {
			result = Double.parseDouble(para);
		} else if ("long".equals(clsName) || "java.lang.Long".equals(clsName)) {
			result = Long.parseLong(para);
		}
		return result;
	}

	/**
	 * 获得简单java bean的属性对应set方法的map
	 * 
	 * @param cls
	 * @return
	 */
	public static Map<String, Method> getSetMethods(Class<?> cls) {
		Map<String, Method> map = new HashMap<String, Method>();
		for (Method m : cls.getDeclaredMethods()) {
			String key = getSetMethodKey(m);
			if (null != key) {
				map.put(key, m);
			}
		}
		return map;
	}

	/**
	 * 获取set方法的属性名称
	 * 
	 * @param m
	 * @return
	 */
	private static String getSetMethodKey(Method m) {
		String s = m.getName();
		if (s.startsWith("set")) {
			s = s.substring(3);
			return s.substring(0, 1).toLowerCase() + s.substring(1);
		}
		return null;
	}

	/**
	 * 获取参数对象的属性值
	 * 
	 * @param obj
	 * @param propertyName
	 * @return
	 * @throws Exception
	 */
	public static Object getPrivatePropertyValue(Object obj, String propertyName) throws Exception {
		Class<? extends Object> cls = obj.getClass();
		Field field = cls.getDeclaredField(propertyName);
		field.setAccessible(true);
		Object result = field.get(obj);
		if ("java.util.Date".equals(result.getClass().getName())) {
			result = sdf.format((Date) result);
		}
		return result;
	}

}
